package com.probiz.estore.cart.service.impl;

import com.probiz.estore.cart.service.ShoppingcartManager;
import com.probiz.estore.common.model.cart.Shoppingcart;
import com.probiz.estore.common.model.sales.Coupon;
import com.probiz.estore.common.service.ProductService;
import com.probiz.estore.common.service.PromoService;
import com.probiz.estore.core.search.SearchCriteriaBuilder;
import com.probiz.estore.core.util.I18nUtil;
import com.probiz.estore.exception.CouponStateException;
import com.probiz.estore.exception.CouponUseException;
import com.probiz.estore.exception.OutOfStockException;
import com.probiz.estore.exception.ProductNoExistException;
import com.probiz.estore.exception.UnderMinimumPurchaseQuantityException;

public class ShoppingcartManagerImpl implements ShoppingcartManager {

	private PromoService promoService;

	private ProductService productService;

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	/**
	 * @deprecated
	 */
	public void deleteExpirationShoppingcarts() {
	}

	public SearchCriteriaBuilder getSearchCriteriaBuilder(String filterName) {
		return null;
	}

	public void setPromoService(PromoService promoService) {
		this.promoService = promoService;
	}

	public boolean putProductsToCart(Shoppingcart shoppingcart, String productSkuCode, String[] childSkuCodes, Integer quantity) throws OutOfStockException, UnderMinimumPurchaseQuantityException, ProductNoExistException {
		productService.checkProductIsExist(productSkuCode, childSkuCodes);
		productService.checkInventory(productSkuCode, quantity);
		productService.checkMinPurchaseQuantity(productSkuCode, quantity);
		if (childSkuCodes != null) {
			for (String childSkuCode : childSkuCodes) {
				productService.checkInventory(childSkuCode, quantity);
			}

			shoppingcart.addProductItem(productSkuCode, childSkuCodes, quantity);
		} else {
			shoppingcart.addProductItem(productSkuCode, quantity);
		}

		return true;
	}

	public boolean changeQuantitys(Shoppingcart shoppingcart, String[] productSkuCodes, Integer[] quantitys) throws OutOfStockException, UnderMinimumPurchaseQuantityException {
		shoppingcart.updateProductItems(productSkuCodes, quantitys);

		return true;
	}

	public boolean changeQuantity(Shoppingcart shoppingcart, String productSkuCode, Integer quantity) throws OutOfStockException, UnderMinimumPurchaseQuantityException {
		productService.checkInventory(productSkuCode, quantity);
		productService.checkMinPurchaseQuantity(productSkuCode, quantity);
		shoppingcart.updateProductItem(productSkuCode, quantity);
		return true;
	}

	public boolean delGiftCertificateItem(Shoppingcart shoppingcart, String shoppingcartItemGcId) {
		shoppingcart.removeGiftCertificateItem(shoppingcartItemGcId);
		return true;
	}

	public boolean clearShoppingcart(Shoppingcart shoppingcart) {
		shoppingcart.clear();
		return true;
	}

	public boolean delProductItem(Shoppingcart shoppingcart, String productSkuCode) {
		shoppingcart.removeProductItem(productSkuCode);
		return true;
	}

	public boolean unuseCoupon(Shoppingcart shoppingcart) {
		shoppingcart.setUsedCouponNo(null);

		return true;
	}

	public boolean useCoupon(Shoppingcart shoppingcart, String couponNo) throws CouponStateException, CouponUseException {
		short couponState = promoService.checkCoupon(couponNo);
		if (couponState != Coupon.STATE_VALID) {
			throw new CouponStateException(couponNo, couponState);
		}

		shoppingcart.setUsedCouponNo(couponNo);
		if (!shoppingcart.isCouponApplied()) {
			// 如果优惠券没有生效则抛异常
			throw new CouponUseException(I18nUtil.getInstance().getMessage("coupon.error.conditionNotSatisfied", new Object[] { couponNo }));
		}

		return true;
	}

}